package com.hejian.android.a72ggametools.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/1/22.
 */
public class GameDetailImgActivity extends AppCompatActivity{
    private ViewPager gameDetailImg;
    private String bigImgUrl;
    private ImageCache imageCache = new DoubleCache();
    private ImageLoader imageLoader;
    private ArrayList<ImageView> viewList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gamedetail_img);
        initView();
        initData();
        setAdapter();
        setListener();
    }

    private void setListener() {
        for (int i = 0, len = viewList.size();i<len;i++){
            viewList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    private void setAdapter() {
        gameDetailImg.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == (View)object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = viewList.get(position);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }
        });
    }

    private void initData() {
        imageLoader = ImageLoader.getInstance(getApplicationContext(),imageCache);
        Intent intent = getIntent();
        bigImgUrl = intent.getStringExtra("bigimgurl");
        String[] urls = bigImgUrl.split(",");
        viewList = new ArrayList<ImageView>();
        for (int i = 0 ,len = urls.length;i<len;i++){
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageLoader.displayImage(urls[i],imageView);

            viewList.add(imageView);
        }


    }

    private void initView() {
        gameDetailImg = (ViewPager)findViewById(R.id.gamedetail_bigimg);
    }
}
